/*
 *             Copyright (C) 2024 Steve Jrong
 * 
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.aa.component.adapter.filter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;

import reactor.core.publisher.Mono;

/**
 * Web过滤器 - 认证授权组件的主Web过滤器
 * <p>
 * 用于向响应式上下文内写入客户端HTTP请求中的请求数据信息（由ServerWebExchange类来提供这些信息）。
 * 
 * @author Steve Jrong
 * @since 1.0
 */
@Component
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class AuthcComponentWebFilter implements WebFilter {

	/**
	 * 过滤器的过滤逻辑
	 */
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
		return chain.filter(exchange)
				// 向响应式上下文对象内写入ServerWebExchange对象，以便在自定义的ReactiveHttpRequestContextHolder类中能获取到客户端请求当中的数据信息
				.contextWrite(context -> context.put(ServerWebExchange.class, exchange));// 使用Mono流的contextWrite()方法设值
	}
}
